package com.edu.whty.entity.base;

import com.edu.whty.common.tools.CollectionUtils;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public final class Pagination implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer curPage = 1; //当前页

    private Integer numPerPage = 20; // 每页数量

    private Integer totalCount = 0; // 总数

    private Integer totalPage = 0; // 总页数
    
    private Integer curCount=0;//当前数量

    private List<Integer> pagesGroup = null; // 分页



    public Integer getFirstRow() {
        return ((getCurPage() - 1) * getNumPerPage()) + 1;
    }

    public Integer getLastRow() {
        Integer lastRow = getCurPage() * getNumPerPage();
        return (lastRow > getTotalCount()) ? getTotalCount() : lastRow;
    }

    public List<Integer> getPageGroup() {
        int basePage;
        if (getCurPage() < 5) {
            basePage = 0;
        } else {
            basePage = getCurPage() - 5;
        }
        List<Integer> pageGroup = CollectionUtils.newArrayList();
        int num = getTotalPage() - basePage;
        num = num >= 9 ? 9 : num;
        for (int i = 1; i <= 9; i++) {
            if ((basePage - (9 - num) + i) > 0) {
                pageGroup.add(basePage - (9 - num) + i);
            }
        }
        return pageGroup;
    }

    public Integer getCurPage() {
        if (curPage <= 0) {
            curPage = 1;
        } else if (curPage > getTotalPage() && getTotalPage() > 0) {
            curPage = totalPage;
        }
        return curPage;
    }


    public Integer getNumPerPage() {
        if (numPerPage <= 0)
            numPerPage = 10;
        return numPerPage;
    }



    public Integer getTotalCount() {
        return totalCount;
    }



    public Integer getTotalPage() {
        if (totalCount == 0) {
            totalPage = 0;
        } else {
            if (totalCount % numPerPage == 0) {
                totalPage = (totalCount / numPerPage);
            } else {
                totalPage = (totalCount / numPerPage) + 1;
            }
        }
        return totalPage;
    }




    



}
